﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using Avivo.Models;
using Microsoft.SmartDevice.Connectivity;

namespace Avivo.Views
{
	public partial class MainForm : Form
	{
		private MountModel mounter;

		public MainForm()
		{
			InitializeComponent();

			this.mounter = new MountModel();
		}

		private void tray_Click(object sender, EventArgs e)
		{
			popup.Show();
		}

		private void popup_Opening(object sender, CancelEventArgs e)
		{
			try
			{
				//Populate "Devices" menu
				itemDevices.DropDownItems.Clear();

				var devices = this.mounter.GetDevices();
				foreach (var device in devices)
				{
					bool mounted = this.mounter.IsMounted(device);

					var item = new ToolStripMenuItem();
					item.Text = device.Name;

					//Mount menu item
					var mount = new ToolStripMenuItem();
					mount.Text = "Mount";
					mount.Enabled = !mounted;
					mount.Click += new EventHandler(itemMount_Click);
					mount.Tag = device;
					mount.Font = new System.Drawing.Font("Segoe UI", 9F, FontStyle.Bold);
					item.DropDownItems.Add(mount);

					//Unmount menu item
					var unmount = new ToolStripMenuItem();
					unmount.Text = "Unmount";
					unmount.Enabled = mounted;
					unmount.Click += new EventHandler(itemUnmount_Click);
					unmount.Tag = device;
					unmount.Font = new System.Drawing.Font("Segoe UI", 9F, FontStyle.Bold);
					item.DropDownItems.Add(unmount);

					//Separator
					item.DropDownItems.Add(new ToolStripSeparator());

					//Explore menu item
					var explore = new ToolStripMenuItem();
					explore.Text = "Explore";
					explore.Enabled = mounted;
					explore.Click += new EventHandler(itemExplore_Click);
					explore.Tag = device;
					item.DropDownItems.Add(explore);

					//Register device
					itemDevices.DropDownItems.Add(item);
				}
			}
			catch
			{
			}
		}

		private void itemMount_Click(object sender, EventArgs e)
		{
			var element = sender as ToolStripItem;
			var device = element.Tag as Device;
			if (device != null)
			{
				try
				{
					device.Connect();
				}
				catch
				{
					MessageBox.Show("Check if Windows Phone device is connected, Zune or wpconnect.exe is running, and screen is unlocked.", "Device problem", MessageBoxButtons.OK, MessageBoxIcon.Error);
					return;
				}

				this.mounter.Mount(device, error =>
				{
					if (error == null)
					{
						tray.ShowBalloonTip(1000, "Windows Phone", "Virtual drive mounted to " + this.mounter.GetDriveLetter(device) + ":", ToolTipIcon.Info);
					}
					else
					{
						tray.ShowBalloonTip(3000, "Windows Phone", "Cannot mount virtual drive! " + error.Message, ToolTipIcon.Error);
					}
				});
			}
		}

		private void itemUnmount_Click(object sender, EventArgs e)
		{
			var element = sender as ToolStripItem;
			var device = element.Tag as Device;
			if (device != null)
			{
				try
				{
					this.mounter.Unmount(device);

					tray.ShowBalloonTip(1000, "Windows Phone", "Virtual drive unmounted.", ToolTipIcon.Info);
				}
				catch (Exception ex)
				{
					tray.ShowBalloonTip(3000, "Windows Phone", "Cannot unmount virtual drive! " + ex.Message, ToolTipIcon.Error);
				}
			}
		}

		private void itemExplore_Click(object sender, EventArgs e)
		{
			var element = sender as ToolStripItem;
			var device = element.Tag as Device;
			if (device != null)
			{
				this.mounter.Explore(device);
			}
		}

		private void itemAbout_Click(object sender, EventArgs e)
		{
			Process.Start("http://wp7vd.codeplex.com/");
		}

		private void itemExit_Click(object sender, EventArgs e)
		{
			//Hide tray icon before close
			tray.Visible = false;

			Application.Exit();
		}

		private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			//Hide tray icon before close
			tray.Visible = false;
		}
	}
}
